package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
@AllArgsConstructor
public class DishController {

    private final DishService dishService;

    /**
     * 添加菜品
     * @param dishDTO 菜品条件
     * @return R
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO){
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param pageQueryDTO 条件
     * @return r
     */
    @ApiOperation(value = "菜品分页查询")
    @GetMapping("/page")
    public Result PageQuery(DishPageQueryDTO pageQueryDTO){
        log.info("菜品分页查询:{}",pageQueryDTO);
        PageBean pageBean = dishService.PageQuery(pageQueryDTO);
        return Result.success(pageBean);
    }


    /**
     * 批量删除菜品
     * @param ids 主键
     * @return
     */
    @ApiOperation(value = "批量删除菜品")
    @DeleteMapping
    public Result deleteByIds(List<Long> ids){
        log.info("批量删除菜品:{}",ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id 菜品主键
     * @return
     */
    @ApiOperation(value = "根据id查询菜品")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Dish dish = dishService.selectById(id);
        return Result.success(dish);
    }

    /**
     * 根据分类查询菜品
     * @param id 分类主键
     * @return R
     */
    @ApiOperation(value = "根据分类查询菜品")
    @GetMapping("/list")
    public Result selectByCategoryId(@PathVariable Integer id){
        Dish dish = dishService.selectByCategory(id);
        return Result.success(dish);
    }

    /**
     *修改菜品状态
     * @param status 状态码
     * @param id 菜品主键
     * @return R
     */
    @ApiOperation(value = "菜品起售/停售")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status ,Integer id){
        dishService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }
}
